package com.yhx.controller;

import com.alibaba.excel.EasyExcel;
import com.yhx.iservice.ISetmealService;
import com.yhx.pojo.Setmeal;
import com.yhx.utils.*;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Reference
    private ISetmealService iSetmealService;
    @Value("${download.path}")
    private String path;

    @Value("${spring.redis.pic}")
    private String pic;
    @Value("${spring.redis.db}")
    private String db;
    @Value("${spring.redis.excel}")
    private String excel;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 将文件名存入reids中
     * @param path
     * @param fileName
     */
    public void saveFileimgname(String path,String fileName){
        SetOperations<String, String> opsForSet = stringRedisTemplate.opsForSet();
        opsForSet.add(path, fileName);
    }

    @PostMapping("listpage")
    public PageResult listPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult = iSetmealService.pageList(queryPageBean);
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    @PostMapping
    public Result save(@RequestBody Setmeal setmeal){
        try {
            Result save = iSetmealService.save(setmeal);
            //将保存的图片存入redis
            saveFileimgname(db,setmeal.getImg());
            return save;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    @GetMapping("/{id}")
    public Result getByid(@PathVariable int id){
        try {
            Setmeal byid = iSetmealService.getByid(id);
            return new Result(true, MessageConstant.QUERY_SETMEAL_FAIL,byid);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
    @PutMapping
    public Result update(@RequestBody Setmeal setmeal){
        try {
            Setmeal byid = iSetmealService.getByid(setmeal.getId());
            //将原来的图片存入pic_list
            saveFileimgname(pic,byid.getImg());
            Result update = iSetmealService.update(setmeal);
            //将新增过来的图片存入db_list
            saveFileimgname(db,setmeal.getImg());
            return update;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_SETMEAL_FAIL);
        }

    }
    @DeleteMapping
    public Result delete(Integer[] ids){
        try {
            Result delete = iSetmealService.delete(ids);
            return delete;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/download")
    public ResponseEntity<byte[]> download(QueryPageBean queryPageBean) throws IOException {
        System.out.println(queryPageBean);
        if("null".equals(queryPageBean.getQueryString())){
            queryPageBean.setQueryString("");
        }
        //查询要下载的数据
        List<Setmeal> rows = iSetmealService.pageList(queryPageBean).getRows();
        System.out.println(rows);
        //把数据写到Excel中
        File file = listToExcel(rows);
        //调用下载
        ResponseEntity<byte[]> download = MyFileUtils.download(file.getName(), path);

        saveFileimgname(excel, file.getName());
        return download;
    }

    @GetMapping("/count")
    public Result countSetmeal(){
        try {
            List<Map<String, Object>> maps = iSetmealService.countSetmeal();
            return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,maps);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }

    }
    private File listToExcel(List<Setmeal> rows){
        String format = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
        String substring = String.valueOf(Math.random()).substring(3, 7);
        File file = new File(path+format+substring+".xlsx");
        EasyExcel.write(file,Setmeal.class).sheet("套餐数据").doWrite(rows);
        return file;
    }
}
